import 'package:barcode_scan2/barcode_scan2.dart';
import 'package:barcode_widget/barcode_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/helper.dart';
import 'package:yp/router.dart';
import 'package:yp/services/api.dart';
import 'package:yp/config/config.dart';

import 'logic.dart';
import 'state.dart';

class MyCouponPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MyCouponPage();
  }
}

class _MyCouponPage extends State<MyCouponPage> {
  final MyCouponLogic logic = Get.put(MyCouponLogic());
  final MyCouponState state = Get.find<MyCouponLogic>().state;
  TextEditingController _editingController = new TextEditingController();
  String editText = '';

  @override
  void initState() {
    super.initState();

    _editingController.addListener(() {
      ///获取输入的内容
      setState(() {
        editText = _editingController.text;
      });
      print(" controller 兼听 ${editText}");
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xfff5f5f5),
      appBar: AppBar(
        centerTitle: true,
        elevation: setWidth(1),
        backgroundColor: Colors.white,
        title: Text(
          '我的优惠券',
          style: TextStyle(
            color: Colors.black,
            fontSize: 18,
          ),
        ),
        iconTheme: IconThemeData(
          color: Color(0xff666666), //修改颜色
        ),
      ),
      body: GetBuilder<MyCouponLogic>(
        builder: (logic) {
          return Container(
            child: Column(
              children: [
                Container(
                  height: setWidth(88),
                  color: Colors.white,
                  child: Row(
                    children: [
                      InkWell(
                        onTap: () {
                          print('===== 可用优惠券(4)点击事件 =====');
                          logic.onChooseCategory(0);
                        },
                        child: Container(
                          width: setWidth(374),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              Container(
                                height: setWidth(84),
                                alignment: Alignment.center,
                                child: Text(
                                  '可用优惠券(${state.availableCouponsIndex})',
                                  style: TextStyle(
                                    color: state.categoryIndex == 0
                                        ? Color(0xffc99f4f)
                                        : Color(0xff999999),
                                    fontSize: 14,
                                  ),
                                ),
                              ),
                              Container(
                                height: setWidth(4),
                                width: setWidth(100),
                                color: state.categoryIndex == 0
                                    ? Color(0xffc99f4f)
                                    : Colors.transparent,
                              ),
                            ],
                          ),
                        ),
                      ),
                      SizedBox(
                        width: setWidth(2),
                      ),
                      InkWell(
                        onTap: () {
                          print('===== 历史优惠券(2)点击事件 =====');
                          logic.onChooseCategory(1);
                        },
                        child: Container(
                          width: setWidth(374),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              Container(
                                height: setWidth(84),
                                alignment: Alignment.center,
                                child: Text(
                                  '历史优惠券(${state.invalidCouponsIndex})',
                                  style: TextStyle(
                                    color: state.categoryIndex == 0
                                        ? Color(0xff999999)
                                        : Color(0xffc99f4f),
                                    fontSize: 14,
                                  ),
                                ),
                              ),
                              Container(
                                height: setWidth(4),
                                width: setWidth(100),
                                color: state.categoryIndex == 0
                                    ? Colors.transparent
                                    : Color(0xffc99f4f),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                // TextField(),
                Visibility(
                  visible: state.categoryIndex == 0,
                  child: Container(
                      width: double.infinity,
                      height: 100.w,
                      child: Row(
                        children: [
                          Container(
                            decoration: new BoxDecoration(
                              color: Colors.white,
                              //设置四周圆角 角度
                              borderRadius:
                                  BorderRadius.all(Radius.circular(4.0)),
                              //设置四周边框
                              border: new Border.all(
                                  width: 1, color: Colors.grey[200]),
                            ),
                            width: 600.w,
                            height: 80.w,
                            child: TextField(
                              controller: _editingController,
                              decoration: InputDecoration(
                                prefixIcon: GestureDetector(
                                  onTap: () async {
                                    var result = await BarcodeScanner.scan();
                                    _editingController.text = result.rawContent;
                                    // toast(result.rawContent);
                                  },
                                  child: Icon(Icons.qr_code_scanner),
                                ),
                                hintText: "请输入兑换码",
                              ),
                            ),
                          ),
                          SizedBox(
                            width: 10.w,
                          ),
                          GestureDetector(
                            onTap: () {
                              if (editText == null || editText.length > 0) {
                                var params = {
                                  'qbarcode': editText,
                                  'access_token': SpUtil.getString('token'),
                                  'usr_num_id': SpUtil.getInt('empeNumId'),
                                };
                                Api.infosync(params).listen((event) {
                                  if (event['code'] == 0) {
                                    logic.onChooseCategory(state.categoryIndex);
                                    toast('领取成功');
                                  }
                                });
                              }
                            },
                            child: Container(
                              decoration: new BoxDecoration(
                                color: editText == null || editText.length > 0
                                    ? Colors.deepOrangeAccent
                                    : Colors.grey[300],
                                //设置四周圆角 角度
                                borderRadius:
                                    BorderRadius.all(Radius.circular(4.0)),
                                //设置四周边框
                                border: new Border.all(
                                    width: 1, color: Colors.grey[200]),
                              ),
                              width: 120.w,
                              height: 80.w,
                              child: Center(
                                  child: Text('兑换',
                                      style: TextStyle(color: Colors.white))),
                            ),
                          )
                        ],
                      )),
                ),
                // Row(
                //   children: [
                //   TextField()
                //   ],
                // ),
                Expanded(
                  child: Container(
                    child: state.categoryIndex == 0
                        ? (state.availableCoupons.length > 0
                            ? SmartRefresher(
                                controller: state.availableCouponsController,
                                header: CustomHeader(
                                  builder: (BuildContext context,
                                      RefreshStatus mode) {
                                    Widget body;
                                    if (mode == RefreshStatus.completed) {
                                      body = CupertinoActivityIndicator();
                                    }
                                    return Container(
                                      height: 55.0,
                                      child: Center(child: body),
                                    );
                                  },
                                ),
                                footer: CustomFooter(
                                  builder:
                                      (BuildContext context, LoadStatus mode) {
                                    Widget body;
                                    if (mode == LoadStatus.idle) {
                                      body = Text("没有更多数据了");
                                    } else if (mode == LoadStatus.loading) {
                                      body = CupertinoActivityIndicator();
                                    } else if (mode == LoadStatus.failed) {
                                      body = Text("刷新失败");
                                    } else {
                                      body = Text("没有更多数据了");
                                    }
                                    return Container(
                                      height: 55.0,
                                      child: Center(child: body),
                                    );
                                  },
                                ),
                                enablePullUp: true,
                                enablePullDown: true,
                                onRefresh: logic.refreshData,
                                onLoading: logic.loadMoreData,
                                child: ListView.separated(
                                  scrollDirection: Axis.vertical,
                                  padding: EdgeInsets.symmetric(
                                      vertical: setWidth(20)),
                                  itemBuilder: (context, index) {
                                    return _availableCouponsWidget(
                                        index: index);
                                  },
                                  separatorBuilder:
                                      (BuildContext context, int index) {
                                    return SizedBox(
                                      height: setWidth(20),
                                    );
                                  },
                                  itemCount: state.availableCoupons.length,
                                ),
                              )
                            : Center(
                                child: Column(
                                  children: [
                                    SizedBox(
                                      height: 140.w,
                                    ),
                                    Image(
                                      image: AssetImage(
                                          "assets/images/nodata.png"),
                                      width: 218.w,
                                      height: 164.w,
                                    ),
                                    SizedBox(
                                      height: 20.w,
                                    ),
                                    Text(
                                      '暂无数据',
                                      style: TextStyle(
                                        color: Color(0xff999999),
                                        fontSize: 28.w,
                                      ),
                                    ),
                                  ],
                                ),
                              ))
                        : (state.invalidCoupons.length > 0
                            ? SmartRefresher(
                                controller: state.availableCouponsController,
                                // controller: state.invalidCouponsController,
                                header: CustomHeader(
                                  builder: (BuildContext context,
                                      RefreshStatus mode) {
                                    Widget body;
                                    if (mode == RefreshStatus.completed) {
                                      body = CupertinoActivityIndicator();
                                    }
                                    return Container(
                                      height: 55.0,
                                      child: Center(child: body),
                                    );
                                  },
                                ),
                                footer: CustomFooter(
                                  builder:
                                      (BuildContext context, LoadStatus mode) {
                                    Widget body;
                                    if (mode == LoadStatus.idle) {
                                      body = Text("没有更多数据了");
                                    } else if (mode == LoadStatus.loading) {
                                      body = CupertinoActivityIndicator();
                                    } else if (mode == LoadStatus.failed) {
                                      body = Text("刷新失败");
                                    } else {
                                      body = Text("没有更多数据了");
                                    }
                                    return Container(
                                      height: 55.0,
                                      child: Center(child: body),
                                    );
                                  },
                                ),
                                enablePullUp: true,
                                enablePullDown: true,
                                onRefresh: logic.refreshData,
                                onLoading: logic.loadMoreData,
                                child: ListView.separated(
                                  scrollDirection: Axis.vertical,
                                  padding: EdgeInsets.symmetric(
                                      vertical: setWidth(20)),
                                  itemBuilder: (context, index) {
                                    return _historicalCouponsWidget(
                                        index: index);
                                  },
                                  separatorBuilder:
                                      (BuildContext context, int index) {
                                    return SizedBox(
                                      height: setWidth(20),
                                    );
                                  },
                                  itemCount: state.invalidCoupons.length,
                                ),
                              )
                            : Center(
                                child: Column(
                                  children: [
                                    SizedBox(
                                      height: 140.w,
                                    ),
                                    Image(
                                      image: AssetImage(
                                          "assets/images/nodata.png"),
                                      width: 218.w,
                                      height: 164.w,
                                    ),
                                    SizedBox(
                                      height: 20.w,
                                    ),
                                    Text(
                                      '暂无数据',
                                      /* 历史 ${state.invalidCoupons.length}*/
                                      style: TextStyle(
                                        color: Color(0xff999999),
                                        fontSize: 28.w,
                                      ),
                                    ),
                                  ],
                                ),
                              )),
                  ),
                ),
                InkWell(
                  onTap: () {
                    print('============= ！去领券中心点击事件！ =============');
                    Get.offNamed(RouteConfig.collectCouponsCentre);
                    // Get.toNamed(RouteConfig.collectCouponsCentre).then((value) {
                    //   // print('============= ！去领券中心返回事件！ ============= value = $value');
                    //   // logic.onReady();
                    // });
                  },
                  child: Container(
                    height: setWidth(100),
                    color: Color(0xffc99f4f),
                    alignment: Alignment.center,
                    child: Text(
                      '去领券中心',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 18,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          );
        },
      ),
    );
  }

  // 可用优惠券
  Widget _availableCouponsWidget({
    int index,
  }) {
    Map<String, dynamic> dataMap = state.availableCoupons[index];
    return index == state.availableCouponsChoose
        ? InkWell(
            onTap: () {
              setState(() {
                if (index == state.availableCouponsChoose) {
                  state.availableCouponsChoose = -1;
                } else {
                  state.availableCouponsChoose = index;
                }
              });

              print(">>>>>>>>>>>");
            },
            child: Stack(
              children: [
                Container(
                  width: MediaQuery.of(context).size.width,
                  margin: EdgeInsets.symmetric(horizontal: setWidth(20)),
                  decoration: BoxDecoration(
                    color: Color(0xfffcfcfc),
                    borderRadius: BorderRadius.circular(setWidth(10)),
                  ),
                  padding: EdgeInsets.symmetric(horizontal: setWidth(32)),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      SizedBox(
                        height: setWidth(226),
                      ),
                      Text(
                        '券编号：${dataMap['qbarcode']}\n'
                        '适用商品：${dataMap['is_all_items'] == true ? '全部商品可用' : '部分商品可用'}\n'
                        '适用渠道：${dataMap['applicable_platform_string'] ?? ''}',
                        // '适用等级：全部等级',
                        style: TextStyle(
                          color: Color(0xff999999),
                          fontSize: 12,
                          height: 1.7,
                        ),
                      ),
                      SizedBox(
                        height: setWidth(16),
                      ),
                      Offstage(
                        offstage: dataMap['share_other_sign'] == 0,
                        child: InkWell(
                          onTap: () {
                            print('========== 转赠点击事件 ==========');
                            showDialog(
                              context: context,
                              builder: (ctx) => CounterWidget(
                                qbarCode: '${dataMap['qbarcode']}',
                                chooseIndex: index,
                              ),
                            );
                          },
                          child: Container(
                            height: setWidth(40),
                            width: setWidth(120),
                            decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius:
                                    BorderRadius.circular(setWidth(20)),
                                border: Border.all(
                                  width: setWidth(1),
                                  color: Color(0xffc99f4f),
                                )),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Image.asset(
                                  'assets/images/locationIcon.png',
                                  width: setWidth(20),
                                  height: setWidth(20),
                                  color: Color(0xffC99F4F),
                                  fit: BoxFit.fill,
                                ),
                                SizedBox(
                                  width: setWidth(8),
                                ),
                                Text(
                                  '转赠',
                                  style: TextStyle(
                                    color: Color(0xffC99F4F),
                                    fontSize: 11,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),
                      SizedBox(
                        height: setWidth(40),
                      ),
                    ],
                  ),
                ),
                Stack(
                  children: [
                    // 背景图
                    Padding(
                      padding: EdgeInsets.only(
                        left: setWidth(16),
                        right: setWidth(16),
                      ),
                      child: Image.asset(
                        'assets/images/couponbg.png',
                        width: setWidth(718),
                        height: setWidth(200),
                        fit: BoxFit.cover,
                      ),
                    ),
                    // 券信息
                    Padding(
                      padding: EdgeInsets.symmetric(horizontal: setWidth(20)),
                      child: Row(
                        children: [
                          Container(
                            width: setWidth(206),
                            height: setWidth(200),
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                // 券左上角 标识
                                Container(
                                  height: setWidth(32),
                                  width: setWidth(80),
                                  decoration: BoxDecoration(
                                    gradient: LinearGradient(
                                      begin: Alignment.centerLeft,
                                      end: Alignment.centerRight,
                                      colors: [
                                        Color(0xff767676),
                                        Color(0xff2B1D06),
                                      ],
                                    ),
                                    borderRadius: BorderRadius.only(
                                      topLeft: Radius.circular(setWidth(8)),
                                      bottomRight: Radius.circular(setWidth(8)),
                                    ),
                                  ),
                                  alignment: Alignment.center,
                                  child: Text(
                                    dataMap['qtype'] == '1'
                                        ? '现金券'
                                        : dataMap['qtype'] == '2'
                                            ? '免邮券'
                                            : '折扣券',
                                    style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 10,
                                    ),
                                  ),
                                ),
                                // 券额
                                Container(
                                  width: setWidth(206),
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                    children: [
                                      // 面额
                                      dataMap['qtype'] == '0'
                                          ? (Text.rich(
                                              TextSpan(
                                                children: [
                                                  TextSpan(
                                                    text:
                                                        '${double.parse((dataMap['qmz'] / 10).toStringAsFixed(2)).toString().replaceAll('.00', '').replaceAll('.0', '') ?? '0'}',
                                                    style: TextStyle(
                                                      color: Color(0xffC99F4F)
                                                          .withOpacity(0.4),
                                                      fontSize: 20,
                                                      fontWeight:
                                                          FontWeight.bold,
                                                    ),
                                                  ),
                                                  TextSpan(
                                                    text: '折',
                                                    style: TextStyle(
                                                      color: Color(0xffC99F4F)
                                                          .withOpacity(0.4),
                                                      fontSize: 16,
                                                    ),
                                                  ),
                                                ],
                                              ),
                                            ))
                                          : dataMap['qtype'] == '1'
                                              ? (Text.rich(
                                                  TextSpan(
                                                    children: [
                                                      TextSpan(
                                                        text: '¥',
                                                        style: TextStyle(
                                                          color: Color(
                                                                  0xffC99F4F)
                                                              .withOpacity(0.4),
                                                          fontSize: 16,
                                                        ),
                                                      ),
                                                      TextSpan(
                                                        text:
                                                            '${dataMap['qmz'].toString().replaceAll('.0', '') ?? '0'}',
                                                        style: TextStyle(
                                                          color: Color(
                                                                  0xffC99F4F)
                                                              .withOpacity(0.4),
                                                          fontSize: 20,
                                                          fontWeight:
                                                              FontWeight.bold,
                                                        ),
                                                      ),
                                                    ],
                                                  ),
                                                ))
                                              : (Text.rich(
                                                  TextSpan(
                                                    children: [
                                                      TextSpan(
                                                        text: '免邮',
                                                        style: TextStyle(
                                                          color: Color(
                                                                  0xffC99F4F)
                                                              .withOpacity(0.4),
                                                          fontSize: 32,
                                                          fontWeight:
                                                              FontWeight.bold,
                                                        ),
                                                      ),
                                                    ],
                                                  ),
                                                )),
                                      // 满额可用
                                      Text(
                                        dataMap['least_cost'] != null &&
                                                dataMap['least_cost'] > 0
                                            ? '满${dataMap['least_cost']}元可用'
                                            : '无限制使用',
                                        style: TextStyle(
                                          color: Color(0xffc99f4f),
                                          fontSize: 11,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                SizedBox(
                                  height: setWidth(20),
                                ),
                              ],
                            ),
                          ),
                          Container(
                            width: setWidth(504),
                            height: setWidth(200),
                            padding: EdgeInsets.only(
                              top: setWidth(18),
                            ),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Padding(
                                  padding: EdgeInsets.only(
                                    left: setWidth(28),
                                    right: setWidth(64),
                                  ),
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      // 券名称
                                      Text.rich(
                                        TextSpan(
                                          children: [
                                            // // 提示
                                            // WidgetSpan(
                                            //   child: Container(
                                            //     width: setWidth(56),
                                            //     height: setWidth(26),
                                            //     decoration: BoxDecoration(
                                            //       color: Color(0xffde9005),
                                            //       borderRadius: BorderRadius.circular(setWidth(14)),
                                            //     ),
                                            //     alignment: Alignment.center,
                                            //     child: Text(
                                            //       '线下',
                                            //       style: TextStyle(
                                            //         color: Colors.white,
                                            //         fontSize: 9,
                                            //       ),
                                            //     ),
                                            //   ),
                                            // ),
                                            TextSpan(
                                              text: '${dataMap['qname']}',
                                              style: TextStyle(
                                                color: Colors.black,
                                                fontSize: 13,
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                      // 开始时间 -- 结束时间
                                      Padding(
                                        padding: EdgeInsets.only(
                                          top: setWidth(10),
                                          bottom: setWidth(18),
                                        ),
                                        child: Text(
                                          '${DateFormat('yyyy.MM.dd').format(DateTime.parse(dataMap['sdate']))}-${DateFormat('yyyy.MM.dd').format(DateTime.parse(dataMap['edate']))}',
                                          style: TextStyle(
                                            color: Color(0xff999999),
                                            fontSize: 10,
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Padding(
                                      padding: EdgeInsets.only(
                                        left: setWidth(28),
                                        right: setWidth(22),
                                      ),
                                      child: Container(
                                        height: setWidth(1),
                                        color: Color(0xffe4e4e4),
                                      ),
                                    ),
                                    Container(
                                      padding: EdgeInsets.only(
                                        left: setWidth(28),
                                        right: setWidth(22),
                                      ),
                                      height: setWidth(56),
                                      child: Row(
                                        children: [
                                          Text(
                                            '查看券说明',
                                            style: TextStyle(
                                              color: Color(0xff666666),
                                              fontSize: 11,
                                            ),
                                          ),
                                          SizedBox(
                                            width: setWidth(8),
                                          ),
                                          Image.asset(
                                            'assets/images/icon-down.png',
                                            width: setWidth(14),
                                            height: setWidth(8),
                                            color: Color(0xff666666),
                                            fit: BoxFit.cover,
                                          )
                                        ],
                                      ),
                                    ),
                                  ],
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
                // Positioned(
                //   top: 0,
                //   right: setWidth(20),
                //   child: Container(
                //     width: setWidth(64),
                //     height: setWidth(64),
                //     child: Image.asset(
                //       'assets/images/qrCode_image.png',
                //       width: setWidth(64),
                //       height: setWidth(64),
                //       fit: BoxFit.cover,
                //     ),
                //   ),
                // ),
              ],
            ),
          )
        : InkWell(
            onTap: () {
              var params = {
                'access_token': SpUtil.getString('token'),
                'series': dataMap["activityid"],
              };
              EasyLoading.show();
              Api.cardmeidacouponruleget(params).listen((event) {
                EasyLoading.dismiss();

                if (event['code'] == 0) {
                  print(event["apply_types"]);
                  var apply_types = event["apply_types"];
                  String md = "全部店铺";
                  if (apply_types['1002'] != null) {
                    md = apply_types['1002'][0]["apply_type_value"];
                  }
                  showDialog(
                    context: context,
                    builder: (context) {
                      return Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Column(
                            children: [
                              Container(
                                width: 600.w,
                                padding: EdgeInsets.symmetric(vertical: 20.w),
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.only(
                                    topLeft: Radius.circular(15),
                                    topRight: Radius.circular(15),
                                  ),
                                ),
                                child: Column(
                                  children: [
                                    Text(
                                      dataMap['qname'],
                                      style: TextStyle(
                                        color: Color(0xff000000),
                                        fontSize: 30.sp,
                                      ),
                                    ),
                                    SizedBox(height: 40.w),
                                    Text(
                                      "券号${dataMap['qbarcode']}",
                                      style: TextStyle(
                                        color: Color(0xff666666),
                                        fontSize: 24.sp,
                                      ),
                                    ),
                                    BarcodeWidget(
                                      data: "${dataMap['qbarcode']}",
                                      barcode: Barcode.code128(),
                                      width: setWidth(440),
                                      height: setWidth(130),
                                      drawText: false,
                                    )
                                  ],
                                ),
                              ),
                              Container(
                                width: 600.w,
                                padding: EdgeInsets.only(
                                    top: 20.w, bottom: 20.w, left: 20.w),
                                decoration: BoxDecoration(
                                  color: Color(0xfff8f8f8),
                                  borderRadius: BorderRadius.only(
                                    bottomLeft: Radius.circular(15),
                                    bottomRight: Radius.circular(15),
                                  ),
                                ),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      "可用时间：${DateFormat('yyyy.MM.dd').format(DateTime.parse(dataMap['sdate']))}-${DateFormat('yyyy.MM.dd').format(DateTime.parse(dataMap['edate']))}",
                                      style: TextStyle(
                                        color: Color(0xff666666),
                                        fontSize: 24.sp,
                                        height: 1.5,
                                      ),
                                    ),
                                    Text(
                                      "适用商品：${dataMap['is_all_items'] == true ? '全部商品可用' : '部分商品可用'}",
                                      style: TextStyle(
                                        color: Color(0xff666666),
                                        fontSize: 24.sp,
                                        height: 1.5,
                                      ),
                                    ),
                                    Text(
                                      '适用店铺：$md',
                                      style: TextStyle(
                                        color: Color(0xff666666),
                                        fontSize: 24.sp,
                                        height: 1.5,
                                      ),
                                    ),
                                    Text(
                                      '适用渠道：${dataMap['applicable_platform_string'] ?? ''} ',
                                      style: TextStyle(
                                        color: Color(0xff666666),
                                        fontSize: 24.sp,
                                        height: 1.5,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                          SizedBox(height: 30.w),
                          GestureDetector(
                            onTap: () {
                              Get.back();
                            },
                            child: Image.asset(
                              "assets/images/close.png",
                              color: Colors.white,
                              width: 60.w,
                            ),
                          )
                        ],
                      );
                    },
                  );
                }
              });

              return;
            },
            child: Stack(
              children: [
                Stack(
                  children: [
                    // 背景图
                    Padding(
                      padding: EdgeInsets.only(
                        left: setWidth(16),
                        right: setWidth(16),
                      ),
                      child: Image.asset(
                        'assets/images/couponbg.png',
                        width: setWidth(718),
                        height: setWidth(200),
                        fit: BoxFit.cover,
                      ),
                    ),
                    // 券信息
                    Padding(
                      padding: EdgeInsets.symmetric(horizontal: setWidth(20)),
                      child: Row(
                        children: [
                          Container(
                            width: setWidth(206),
                            height: setWidth(200),
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                // 券左上角 标识
                                Container(
                                  height: setWidth(32),
                                  width: setWidth(80),
                                  decoration: BoxDecoration(
                                    gradient: LinearGradient(
                                      begin: Alignment.centerLeft,
                                      end: Alignment.centerRight,
                                      colors: [
                                        Color(0xff767676),
                                        Color(0xff2B1D06),
                                      ],
                                      // colors: [
                                      //   Color(0xff03B465),
                                      //   Color(0xff07DE7D),
                                      // ],
                                    ),
                                    borderRadius: BorderRadius.only(
                                      topLeft: Radius.circular(setWidth(8)),
                                      bottomRight: Radius.circular(setWidth(8)),
                                    ),
                                  ),
                                  alignment: Alignment.center,
                                  child: Text(
                                    dataMap['qtype'] == '1'
                                        ? '现金券'
                                        : dataMap['qtype'] == '2'
                                            ? '免邮券'
                                            : '折扣券',
                                    style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 10,
                                    ),
                                  ),
                                ),
                                // 券额
                                Container(
                                  width: setWidth(206),
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                    children: [
                                      dataMap['qtype'] == '0'
                                          ? (Text.rich(
                                              TextSpan(
                                                children: [
                                                  TextSpan(
                                                    text:
                                                        '${double.parse((dataMap['qmz'] / 10).toStringAsFixed(2)).toString().replaceAll('.00', '').replaceAll('.0', '') ?? '0'}',
                                                    style: TextStyle(
                                                      color: Color(0xffC99F4F)
                                                          .withOpacity(0.4),
                                                      fontSize: 20,
                                                      fontWeight:
                                                          FontWeight.bold,
                                                    ),
                                                  ),
                                                  TextSpan(
                                                    text: '折',
                                                    style: TextStyle(
                                                      color: Color(0xffC99F4F)
                                                          .withOpacity(0.4),
                                                      fontSize: 16,
                                                    ),
                                                  ),
                                                ],
                                              ),
                                            ))
                                          : dataMap['qtype'] == '1'
                                              ? (Text.rich(
                                                  TextSpan(
                                                    children: [
                                                      TextSpan(
                                                        text: '¥',
                                                        style: TextStyle(
                                                          color: Color(
                                                                  0xffC99F4F)
                                                              .withOpacity(0.4),
                                                          fontSize: 16,
                                                        ),
                                                      ),
                                                      TextSpan(
                                                        text:
                                                            '${dataMap['qmz'].toString().replaceAll('.0', '') ?? '0'}',
                                                        style: TextStyle(
                                                          color: Color(
                                                                  0xffC99F4F)
                                                              .withOpacity(0.4),
                                                          fontSize: 20,
                                                          fontWeight:
                                                              FontWeight.bold,
                                                        ),
                                                      ),
                                                    ],
                                                  ),
                                                ))
                                              : (Text.rich(
                                                  TextSpan(
                                                    children: [
                                                      TextSpan(
                                                        text: '免邮',
                                                        style: TextStyle(
                                                          color: Color(
                                                                  0xffC99F4F)
                                                              .withOpacity(0.4),
                                                          fontSize: 32,
                                                          fontWeight:
                                                              FontWeight.bold,
                                                        ),
                                                      ),
                                                    ],
                                                  ),
                                                )),
                                      Text(
                                        dataMap['least_cost'] != null &&
                                                dataMap['least_cost'] > 0
                                            ? '满${dataMap['least_cost']}元可用'
                                            : '无限制使用',
                                        style: TextStyle(
                                          color: Color(0xffc99f4f),
                                          fontSize: 11,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                SizedBox(
                                  height: setWidth(20),
                                ),
                              ],
                            ),
                          ),
                          Container(
                            width: setWidth(444),
                            height: setWidth(200),
                            padding: EdgeInsets.only(
                              top: setWidth(18),
                            ),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Padding(
                                  padding: EdgeInsets.only(
                                    left: setWidth(28),
                                    right: setWidth(64),
                                  ),
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Text.rich(
                                        TextSpan(
                                          children: [
                                            // WidgetSpan(
                                            //   child: Container(
                                            //     width: setWidth(56),
                                            //     height: setWidth(26),
                                            //     decoration: BoxDecoration(
                                            //       color: Color(0xffde9005),
                                            //       borderRadius: BorderRadius.circular(setWidth(14)),
                                            //     ),
                                            //     alignment: Alignment.center,
                                            //     child: Text(
                                            //       '线下',
                                            //       style: TextStyle(
                                            //         color: Colors.white,
                                            //         fontSize: 9,
                                            //       ),
                                            //     ),
                                            //   ),
                                            // ),
                                            TextSpan(
                                              text: '${dataMap['qname']}',
                                              style: TextStyle(
                                                color: Colors.black,
                                                fontSize: 13,
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                      Padding(
                                        padding: EdgeInsets.only(
                                          top: setWidth(10),
                                          bottom: setWidth(18),
                                        ),
                                        child: Text(
                                          '${DateFormat('yyyy.MM.dd').format(DateTime.parse(dataMap['sdate']))}-${DateFormat('yyyy.MM.dd').format(DateTime.parse(dataMap['edate']))}',
                                          style: TextStyle(
                                            color: Color(0xff999999),
                                            fontSize: 10,
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Padding(
                                      padding: EdgeInsets.only(
                                        left: setWidth(28),
                                        right: setWidth(22),
                                      ),
                                      child: Container(
                                        height: setWidth(1),
                                        color: Color(0xffe4e4e4),
                                      ),
                                    ),
                                    GestureDetector(
                                      onTap: () {
                                        setState(() {
                                          if (index ==
                                              state.availableCouponsChoose) {
                                            state.availableCouponsChoose = -1;
                                          } else {
                                            state.availableCouponsChoose =
                                                index;
                                          }
                                        });
                                      },
                                      child: Container(
                                        padding: EdgeInsets.only(
                                          left: setWidth(28),
                                          right: setWidth(22),
                                        ),
                                        height: setWidth(56),
                                        child: Row(
                                          children: [
                                            Text(
                                              '查看券说明',
                                              style: TextStyle(
                                                color: Color(0xff666666),
                                                fontSize: 11,
                                              ),
                                            ),
                                            SizedBox(
                                              width: setWidth(8),
                                            ),
                                            Image.asset(
                                              'assets/images/icon-down.png',
                                              width: setWidth(14),
                                              height: setWidth(8),
                                              color: Color(0xff666666),
                                              fit: BoxFit.cover,
                                            )
                                          ],
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ],
                            ),
                          ),
                          Container(
                            child: new Material(
                              //INK可以实现装饰容器
                              child: new Ink(
                                //用ink圆角矩形
                                decoration: new BoxDecoration(
                                  //背景
                                  color: Color(0xffc99f4f),
                                  //设置四周圆角 角度
                                  borderRadius: BorderRadius.only(
                                    topRight: Radius.circular(8),
                                    bottomRight: Radius.circular(8),
                                  ),
                                ),
                                child: new InkWell(
                                    //圆角设置,给水波纹也设置同样的圆角
                                    //如果这里不设置就会出现矩形的水波纹效果
                                    borderRadius:
                                        new BorderRadius.circular(25.0),
                                    //设置点击事件回调
                                    onTap: () {
                                      if (dataMap['disable']) {
                                        toast('未到优惠券使用时间');
                                        return false;
                                      }
                                      Get.toNamed(
                                          RouteConfig.couponGoodsList,
                                          arguments: {
                                            'coupon_id':
                                                dataMap['activityid'],
                                          });
                                    },
                                    child: Container(
                                      //设置 child 居中
                                      alignment: Alignment(0, 0),
                                      width: setWidth(60),
                                      height: setWidth(200),
                                      child: Text(
                                        "去使用",
                                        textAlign: TextAlign.center,
                                        style: TextStyle(
                                          fontSize: 32.sp,
                                          color: Colors.white,
                                        ),
                                      ),
                                    )),
                              ),
                            ),
                          )
                        ],
                      ),
                    ),
                  ],
                ),
                // Positioned(
                //   top: 0,
                //   right: setWidth(20),
                //   child: Container(
                //     width: setWidth(64),
                //     height: setWidth(64),
                //     child: Image.asset(
                //       'assets/images/qrCode_image.png',
                //       width: setWidth(64),
                //       height: setWidth(64),
                //       fit: BoxFit.cover,
                //     ),
                //   ),
                // ),
              ],
            ),
          );
  }

  // 历史优惠券
  Widget _historicalCouponsWidget({
    int index,
  }) {
    Map<String, dynamic> dataMap = state.invalidCoupons[index];
    return Stack(
      children: [
        Stack(
          children: [
            // 背景图
            Padding(
              padding: EdgeInsets.only(
                left: setWidth(16),
                right: setWidth(16),
              ),
              child: Image.asset(
                'assets/images/couponbg_image.png',
                width: setWidth(718),
                height: setWidth(200),
                fit: BoxFit.cover,
              ),
            ),
            // 券信息
            Padding(
              padding: EdgeInsets.symmetric(horizontal: setWidth(20)),
              child: Row(
                children: [
                  Container(
                    width: setWidth(206),
                    height: setWidth(200),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        // 券左上角 标识
                        Container(
                          height: setWidth(32),
                          width: setWidth(80),
                          decoration: BoxDecoration(
                            gradient: LinearGradient(
                              begin: Alignment.centerLeft,
                              end: Alignment.centerRight,
                              colors: [
                                Color(0xff767676).withOpacity(0.6),
                                Color(0xff2B1D06).withOpacity(0.6),
                              ],
                              // colors: [
                              //   Color(0xff03B465),
                              //   Color(0xff07DE7D),
                              // ],
                            ),
                            borderRadius: BorderRadius.only(
                              topLeft: Radius.circular(setWidth(8)),
                              bottomRight: Radius.circular(setWidth(8)),
                            ),
                          ),
                          alignment: Alignment.center,
                          child: Text(
                            dataMap['qtype'] == '1'
                                ? '现金券'
                                : dataMap['qtype'] == '2'
                                    ? '免邮券'
                                    : '折扣券',
                            style: TextStyle(
                              color: Colors.white.withOpacity(0.4),
                              fontSize: 10,
                            ),
                          ),
                        ),
                        // 券额
                        Container(
                          width: setWidth(206),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              dataMap['qtype'] == '0'
                                  ? (Text.rich(
                                      TextSpan(
                                        children: [
                                          TextSpan(
                                            text: '${double.parse((dataMap['qmz'] / 10).toStringAsFixed(2)).toString().replaceAll('.00', '').replaceAll('.0', '') ?? '0'}',
                                            style: TextStyle(
                                              color: Color(0xffC99F4F)
                                                  .withOpacity(0.4),
                                              fontSize: 20,
                                              fontWeight: FontWeight.bold,
                                            ),
                                          ),
                                          TextSpan(
                                            text: '折',
                                            style: TextStyle(
                                              color: Color(0xffC99F4F)
                                                  .withOpacity(0.4),
                                              fontSize: 16,
                                            ),
                                          ),
                                        ],
                                      ),
                                    ))
                                  : dataMap['qtype'] == '1'
                                      ? (Text.rich(
                                          TextSpan(
                                            children: [
                                              TextSpan(
                                                text: '¥',
                                                style: TextStyle(
                                                  color: Color(0xffC99F4F)
                                                      .withOpacity(0.4),
                                                  fontSize: 16,
                                                ),
                                              ),
                                              TextSpan(
                                                text:
                                                    '${dataMap['qmz'].toString().replaceAll('.0', '') ?? '0'}',
                                                style: TextStyle(
                                                  color: Color(0xffC99F4F)
                                                      .withOpacity(0.4),
                                                  fontSize: 20,
                                                  fontWeight: FontWeight.bold,
                                                ),
                                              ),
                                            ],
                                          ),
                                        ))
                                      : (Text.rich(
                                          TextSpan(
                                            children: [
                                              TextSpan(
                                                text: '免邮',
                                                style: TextStyle(
                                                  color: Color(0xffC99F4F)
                                                      .withOpacity(0.4),
                                                  fontSize: 32,
                                                  fontWeight: FontWeight.bold,
                                                ),
                                              ),
                                            ],
                                          ),
                                        )),
                              Text(
                                dataMap['least_cost'] != null &&
                                        dataMap['least_cost'] > 0
                                    ? '满${dataMap['least_cost']}元可用'
                                    : '无限制使用',
                                style: TextStyle(
                                  color: Color(0xffc99f4f).withOpacity(0.4),
                                  fontSize: 11,
                                ),
                              ),
                            ],
                          ),
                        ),
                        SizedBox(
                          height: setWidth(20),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    width: setWidth(504),
                    height: setWidth(200),
                    padding: EdgeInsets.only(
                      top: setWidth(18),
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Padding(
                          padding: EdgeInsets.only(
                            left: setWidth(28),
                            right: setWidth(64),
                          ),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text.rich(
                                TextSpan(
                                  children: [
                                    // WidgetSpan(
                                    //   child: Container(
                                    //     width: setWidth(56),
                                    //     height: setWidth(26),
                                    //     decoration: BoxDecoration(
                                    //       color: Color(0xffde9005),
                                    //       borderRadius: BorderRadius.circular(setWidth(14)),
                                    //     ),
                                    //     alignment: Alignment.center,
                                    //     child: Text(
                                    //       '线下',
                                    //       style: TextStyle(
                                    //         color: Colors.white,
                                    //         fontSize: 9,
                                    //       ),
                                    //     ),
                                    //   ),
                                    // ),
                                    TextSpan(
                                      text: '${dataMap['qname']}',
                                      style: TextStyle(
                                        color: Colors.black.withOpacity(0.4),
                                        fontSize: 13,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              Padding(
                                padding: EdgeInsets.only(
                                  top: setWidth(10),
                                  bottom: setWidth(18),
                                ),
                                child: Text(
                                  '${DateFormat('yyyy.MM.dd').format(DateTime.parse(dataMap['sdate']))}-${DateFormat('yyyy.MM.dd').format(DateTime.parse(dataMap['edate']))}',
                                  style: TextStyle(
                                    color: Color(0xff999999).withOpacity(0.4),
                                    fontSize: 10,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Padding(
                              padding: EdgeInsets.only(
                                left: setWidth(28),
                                right: setWidth(22),
                              ),
                              child: Container(
                                height: setWidth(1),
                                color: Color(0xffe4e4e4),
                              ),
                            ),
                            Container(
                              padding: EdgeInsets.only(
                                left: setWidth(28),
                                right: setWidth(22),
                              ),
                              height: setWidth(56),
                              child: Row(
                                children: [
                                  Text(
                                    '查看券说明',
                                    style: TextStyle(
                                      color: Color(0xff666666).withOpacity(0.4),
                                      fontSize: 11,
                                    ),
                                  ),
                                  SizedBox(
                                    width: setWidth(8),
                                  ),
                                  Image.asset(
                                    'assets/images/icon-down.png',
                                    width: setWidth(14),
                                    height: setWidth(8),
                                    color: Color(0xff666666),
                                    fit: BoxFit.cover,
                                  )
                                ],
                              ),
                            ),
                          ],
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
        // Positioned(
        //   top: 0,
        //   right: setWidth(20),
        //   child: Container(
        //     width: setWidth(64),
        //     height: setWidth(64),
        //     child: Image.asset(
        //       'assets/images/qrCode_image.png',
        //       width: setWidth(64),
        //       height: setWidth(64),
        //       fit: BoxFit.cover,
        //     ),
        //   ),
        // ),
      ],
    );
  }
}

class CounterWidget extends StatefulWidget {
  final String qbarCode;
  final int chooseIndex;

  CounterWidget({this.qbarCode, this.chooseIndex});

  @override
  _CounterWidgetState createState() => _CounterWidgetState();
}

class _CounterWidgetState extends State<CounterWidget> {
  final MyCouponLogic logic = Get.put(MyCouponLogic());
  TextEditingController phoneController = TextEditingController();
  bool textBool = false;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.symmetric(horizontal: setWidth(60)),
            child: Stack(
              children: [
                Container(
                  color: Colors.white,
                  width: setWidth(690),
                  // height: setWidth(480),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      SizedBox(
                        height: setWidth(40),
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            '赠送给',
                            style: TextStyle(
                              color: Colors.black,
                              fontSize: 17,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ],
                      ),
                      SizedBox(
                        height: setWidth(20),
                      ),
                      Padding(
                        padding: EdgeInsets.symmetric(horizontal: setWidth(30)),
                        child: Container(
                          height: setWidth(100),
                          color: Color(0xfff0f0f0),
                          padding:
                              EdgeInsets.symmetric(horizontal: setWidth(20)),
                          child: TextField(
                            controller: phoneController,
                            maxLines: 1,
                            style: TextStyle(
                              color: Color(0xff222222),
                              fontSize: 16,
                            ),
                            onChanged: (value) {
                              // logic.textControllerClick();
                              setState(() {
                                textBool = false;
                                if (value.length > 0) {
                                  textBool = true;
                                }
                              });
                            },
                            decoration: InputDecoration(
                              contentPadding: EdgeInsets.zero,
                              hintText: '请输入好友手机号',
                              hintStyle: TextStyle(
                                color: Color(0xffcccccc),
                                fontSize: 16,
                              ),
                              enabledBorder: OutlineInputBorder(
                                // 设置输入框可编辑时的边框样式
                                borderRadius: BorderRadius.all(
                                    Radius.circular(0)), // 设置边框四个角的弧度
                                borderSide: BorderSide(
                                  // 用来配置边框的样式
                                  color: Colors.transparent, // 设置边框的颜色
                                  width: setWidth(2), // 设置边框的粗细
                                ),
                              ),
                              focusedBorder: OutlineInputBorder(
                                // 用来配置输入框获取焦点时的颜色
                                borderRadius: BorderRadius.all(
                                    Radius.circular(0)), // 设置边框四个角的弧度
                                borderSide: BorderSide(
                                  // 用来配置边框的样式
                                  color: Colors.transparent, // 设置边框的颜色
                                  width: setWidth(2), // 设置边框的粗细
                                ),
                              ),
                            ),
                          ),
                        ),
                      ),
                      SizedBox(
                        height: setWidth(16),
                      ),
                      Padding(
                        padding: EdgeInsets.symmetric(horizontal: setWidth(30)),
                        child: Text(
                          '注:转赠对象须是会员',
                          style: TextStyle(
                            color: Color(0xff999999),
                            fontSize: 9,
                          ),
                        ),
                      ),
                      SizedBox(
                        height: setWidth(24),
                      ),
                      Padding(
                        padding: EdgeInsets.symmetric(horizontal: setWidth(30)),
                        child: InkWell(
                          onTap: () {
                            print(' ！！！！立即赠送 点击事件 ！！！！ ');
                            FocusScope.of(context).requestFocus();
                            logic.givingCoupon(
                              qbarcode: widget.qbarCode,
                              phone: phoneController.text,
                              index: widget.chooseIndex,
                            );
                            Navigator.pop(context);
                          },
                          child: Container(
                            decoration: BoxDecoration(
                              color: textBool
                                  ? Color(0xffc99f4f)
                                  : Color(0xfffac8b4),
                              borderRadius: BorderRadius.circular(setWidth(10)),
                            ),
                            height: setWidth(100),
                            alignment: Alignment.center,
                            child: Text(
                              '立即赠送',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 15,
                              ),
                            ),
                          ),
                        ),
                      ),
                      SizedBox(
                        height: setWidth(20),
                      ),
                    ],
                  ),
                ),
                Positioned(
                  top: 0,
                  right: 0,
                  child: InkWell(
                    onTap: () {
                      print(' 隐藏 弹框事件 ！！！');
                    },
                    child: Container(
                      padding: EdgeInsets.all(setWidth(10)),
                      child: Image.asset(
                        'assets/images/close_images.png',
                        width: setWidth(50),
                        height: setWidth(50),
                        fit: BoxFit.fill,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
